package furns.dao.impl;

import furns.dao.BasicDAO;
import furns.dao.FurnDAO;
import furns.entity.Furn;

import java.util.List;

public class FurnDAOImpl extends BasicDAO<Furn> implements FurnDAO {
    @Override
    public List<Furn> queryFurns() {
        String sql = "select id,name,maker,price,sales,stock,img_path imgPath from furn";
        return queryMulti(sql, Furn.class);
    }

    @Override
    public int addFurn(Furn furn) {
        String sql = "insert into furn(id,name,maker,price,sales,stock,img_path) values(?,?,?,?,?,?,?)";
        return update(sql, null, furn.getName(), furn.getMaker(), furn.getPrice(), furn.getSales(), furn.getStock(), furn.getImgPath());
    }

    @Override
    public int delFurnById(int id) {
        String sql = "delete from furn where id = ?";
        return update(sql, id);
    }

    @Override
    public Furn queryFurnById(int id) {
        String sql = "select id,name,maker,price,sales,stock,img_path imgPath from furn where id = ?";
        return querySingle(sql, Furn.class, id);
    }

    @Override
    public int updateFurn(Furn furn) {
        String sql = "update furn set name = ?,maker = ?,price=?,sales=?,stock=?,img_path=? where id = ?";
        return update(sql, furn.getName(), furn.getMaker(), furn.getPrice(), furn.getSales(), furn.getStock(), furn.getImgPath(), furn.getId());
    }

    @Override
    public int getTotalRow() {
        String sql = "select count(*) from furn";
        //return (Integer) queryScalar(sql); //=> cast异常(会返回Long类型的)
        return ((Number) queryScalar(sql)).intValue();
    }

    @Override
    public List<Furn> getPageItem(int begin, int pageSize) {
        String sql = "select id,name,maker,price,sales,stock,img_path imgPath from furn limit ?,?";
        return queryMulti(sql, Furn.class, begin, pageSize);
    }

    @Override
    public int getTotalRowByName(String name) {
        String sql = "select count(*) from furn where name like ?";
        return ((Number) queryScalar(sql, "%" + name + "%")).intValue();
    }

    @Override
    public List<Furn> getPageItemByName(int begin, int pageSize, String name) {
        String sql = "select id,name,maker,price,sales,stock,img_path imgPath from furn where name like ? limit ?,?";
       return queryMulti(sql, Furn.class, "%" + name + "%", begin, pageSize);

    }
}